package com.cody.learn;

import com.alibaba.fastjson.JSON;
import com.cody.learn.dao.impl.StudentDaoImpl;
import com.cody.learn.entity.Category;
import com.cody.learn.entity.Order;
import com.cody.learn.entity.Product;
import com.cody.learn.entity.Student;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

import java.io.IOException;
import java.io.Reader;
import java.sql.Date;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Hello world!
 */
public class App {

    public static void main(String[] args) {
        //System.out.println( "Hello World!" );
        //testStudent();
        testSomething();
    }

    private static void testSomething() {
        SqlMapClient sqlMapClient = null;
        try {
            Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
            sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            //------------------------------------------
            //Product product = (Product) sqlMapClient.queryForObject("Exercise.selectProduct",1);
            //------------------------------------------
            //List<Product> productList = sqlMapClient.queryForList("Exercise.selectAllProduct");
            //System.out.println(JSON.toJSONString(productList,true));

            //------------------------------------------Resolving complex properties with a join
            //Product product = (Product) sqlMapClient.queryForObject("Exercise.selectProduct",1);
            //System.out.println(JSON.toJSONString(product,true));

            //------------------------------------------Mapping that creates a list of complex objects
            //Category category = (Category) sqlMapClient.queryForObject("Exercise.selectCategory",1);
            //System.out.println(JSON.toJSONString(category,true));

            //------------------------------------------Mapping a componsite key
            Order order = (Order) sqlMapClient.queryForObject("Exercise.selectOrder",1);
            System.out.println(JSON.toJSONString(order,true));
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private static void testStudent() {
        StudentDaoImpl studentDaoImpl = new StudentDaoImpl();
        System.out.println("测试插入");
        Student addStudent = new Student();
        addStudent.setName("李四");
        addStudent.setBirth(Date.valueOf("2011-09-02"));
        addStudent.setScore(88);
        System.out.println(studentDaoImpl.addStudent(addStudent));
        System.out.println("测试根据id查询");
        System.out.println(studentDaoImpl.selectStudentById(1));
        System.out.println("测试模糊查询");
        List<Student> mohuLists = studentDaoImpl.selectStudentByName("李");
        for (Student student : mohuLists) {
            System.out.println(student);
        }
        System.out.println("测试查询所有");
        List<Student> students = studentDaoImpl.selectAllStudent();
        for (Student student : students) {
            System.out.println(student);
        }
        System.out.println("根据id删除学生信息");
        System.out.println(studentDaoImpl.deleteStudentById(1));
        System.out.println("测试更新学生信息");
        Student updateStudent = new Student();
        updateStudent.setId(1);
        updateStudent.setName("李四1");
        updateStudent.setBirth(Date.valueOf("2011-08-07"));
        updateStudent.setScore(21);
        System.out.println(studentDaoImpl.updateStudent(updateStudent));
    }

}
